package nl.infodation.emr.pricingservice.ui.page;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import nl.infodation.emr.pricingservice.entity.User;
import nl.infodation.emr.pricingservice.ui.component.AbstractBasePanel;
import nl.infodation.emr.pricingservice.ui.LoginView;
import nl.infodation.emr.pricingservice.ui.MainView;
import nl.infodation.emr.utility.Constants;

import com.vaadin.Application;
import com.vaadin.terminal.Terminal;
import com.vaadin.ui.*;
import com.vaadin.ui.Window.Notification;

public class PricingServiceApplication extends Application {
	protected static final Logger LOG = LoggerFactory.getLogger(PricingServiceApplication.class);
	private static final long serialVersionUID = 1L;
	
	private static String appPath;
	
	private static List<User> userList = new ArrayList<User>();
	
	public static void addUser(User user) {
		if (user != null) {
			userList.add(user);
		}
	}
	
	public static List<User> getUserList() {
		return userList;
	}
	
	public static User getUser(String username) {
		if (username == null)
			return null;
		
		for (User user : userList) {
		    if (username.equals(user.getUsername())) {
		    	return user;
		    }
		}
		return null;
	}
	
	public static void removeUser(String username) {
		if (username == null)
			return;
		
		for (User user : userList) {
		    if (username.equals(user.getUsername())) {
		    	userList.remove(user);
		    	return;
		    }
		}
	}
	
	public static String getAppPath() {
		return appPath;
	}
	
	@Override
	public void init() {
		LOG.info( "Pricing Service is starting......");
		appPath = getContext().getBaseDirectory().getPath();
		
		// Create the application data instance
        SessionData sessionData = new SessionData(this);
        
        // Register it as a listener in the application context
        getContext().addTransactionListener(sessionData);
		setErrorHandler();
		setTheme("Pricing_Layout");
		final Window window = new Window();
		setMainWindow(window);
		setContent(new LoginView());
	}
	
	public void setContent(final AbstractBasePanel panel){
		final Window window = getMainWindow();
		if (panel instanceof LoginView) {
			window.setCaption("Login");
		} else if (panel instanceof MainView) {
			window.setCaption("Rules Manager");
		}
		window.setContent(panel);
	}
	
	private void setErrorHandler() {
		setErrorHandler(new Terminal.ErrorListener() {
			private static final long serialVersionUID = 1L;
	        public void terminalError(final Terminal.ErrorEvent event) {
	                final Throwable throwable = event.getThrowable();
	                
	                // If this is just an Validation exception, we do nothing
	                if (isVaadinValidationException(throwable.getCause())) {
	                	LOG.debug("Ignoring validation exception in Pricing Service application", throwable);
                        return;
	                }
	                LOG.info("", throwable);
	                getMainWindow().showNotification(SessionData.getI18N().getMessage(Constants.ROLLBACK_UNEXPECTED), 
	                                                 throwable.getMessage(), 
	                                                 Notification.TYPE_ERROR_MESSAGE);                              
	        }
		});
	}
	
	private boolean isVaadinValidationException(final Throwable throwable) {
        Boolean result = false;
        if (throwable == null) {
        	result = false;            
        }
        if (throwable instanceof com.vaadin.data.Validator.EmptyValueException 
                        || throwable instanceof com.vaadin.data.Validator.InvalidValueException) {
        	result = true;
        }               
        return result;
    }
}
